<!--
 * @Author: XieQiu 雪球
 * @Date: 2021-05-09 11:07:23
 * @LastEditTime: 2024-05-20 08:57:29
 * @LastEditors: Please set LastEditors
 * @Description: 羊只信息页面
-->
<template>
  <BaseMainSlot
    v-loading="downLoading"
    element-loading-text="拼命下载中"
    element-loading-spinner="el-icon-loading"
    :show-tab="true"
    :show-search-box="false"
  >
    <template v-slot:btn-box>
      <ActionBar
        :page-no="615"
        :add-active="addActive"
        :del-active="delActive"
        :cancel-active="cancelActive"
        :has-del="false"
        :has-add="false"
        :has-cancel="false"
        :has-edit="false"
        :has-save="false"
        :has-fasong="hasFasong"
        :fasong-text="'初始化'"
        @fasong="save"
      />
    </template>
    <template v-slot:tab-select>
      <selectbutton
        :buttonselectlist="traceTypeData"
        @nowselectbuttonindex="nowselectbuttonindex"
      />
    </template>
    <template v-slot:search-box>
      <SearchBox @search="search">
        <div class="common-search-item">
          <div class="label"></div>
          <div class="input"></div>
        </div>
      </SearchBox>
    </template>
    <template v-slot:table-box>
      <el-table
        ref="tableData"
        v-loading="loading"
        highlight-current-row
        :data="tableData"
        stripe
        border
        height="100%"
      >
        <el-table-column type="index" label="序号" width="60" />
        <el-table-column prop="displayFieldName" label="展示名称" />
        <el-table-column prop="fieldName" label="展示字段" />
        <el-table-column prop="displayType" label="展示类型" />
        <el-table-column prop="status" label="是否可用" min-width="150">
          <template v-if="scope.row.displayFieldsId" slot-scope="scope">
            <el-switch
              v-if="judgePermission(615,'edit')"
              v-model="scope.row.displayFlag"
              active-color="#ff4949"
              inactive-color="#13ce66"
              :active-value="false"
              :inactive-value="true"
              @change="isUse(scope.row)"
            />
            <el-switch
              v-else-if="!judgePermission(615,'edit')"
              v-model="scope.row.displayFlag"
              disabled
              active-color="#ff4949"
              inactive-color="#13ce66"
              :active-value="false"
              :inactive-value="true"
            />
          </template>
        </el-table-column>
        <!-- <el-table-column type="selection" width="155" :selectable="(row) => {return !('index' in row)}" /> -->
      </el-table>
    </template>
  </BaseMainSlot>
</template>

<script>
import ActionBar from "@/components/ActionBar";
import SearchBox from "@/components/SearchBox";
import BaseMainSlot from "@/components/BaseMainSlot/newBaseMainSlot";
import { mymixin } from "@/utils/mixins";
import selectbutton from "@/components/selectbutton/index.vue";

import dataModificationApi from "@/api/trace-source-web/base-infor-manage/data-modification-configuration";
export default {
  name: "SalesManufacturerManagement",
  components: {
    ActionBar,
    BaseMainSlot,
    SearchBox,
    selectbutton
  },
  mixins: [mymixin],
  data() {
    return {
      hasFasong: true,
      upLoading: false,
      downLoading: false,
      tableData: [],
      // 类型
      traceTypeData: [
        { type: "批次", name: "批次" },
        { type: "个体", name: "个体" }
      ],
      searchObj: {},
    };
  },
  computed: {
  },
  watch: {
  },
  created () {
    if (['dt', 'dp','sc'].includes(sessionStorage.farmingTypeNo)) {
      this.traceTypeData = [
        { type: "批次", name: "批次" }
      ]
    }
  },
  mounted() {
    this.search();
  },
  methods: {
    // 按钮当前选择index
    nowselectbuttonindex(i) {
      this.topitemtypeid = i;
      this.search();
    },
    getTableDataList() {
      this.loading = true;
      let params = {
        // page: this.currentPage,
        // size: this.pageSize,
        // entity: {
        ...this.searchObj,
        traceType: this.topitemtypeid.type
        // }
      };
      dataModificationApi.conditionQuery(params).then(res => {
        this.loading = false;
        let data = res.data || [];
        // 赋值总条数
        // if (data) {
        // 赋值数组
        this.tableData = JSON.parse(this.livestock(JSON.stringify(data)));
        this.hasFasong = this.tableData && this.tableData.length === 0;
        this.addblankmet();
        this.resetActionBar();
        // }
      });
    },
    // 栋舍是否可用
    isUse(row) {
      dataModificationApi.edit(row).then(res => {
        if (res.code === 1) {
          this.$message.success(res.msg || "修改成功！");
          this.getTableDataList();
        } else {
          this.$message.error(res.msg || "修改失败！");
        }
      });
    },
    toggleRowSelection(row, selected) {
      row.displayFlag = !row.displayFlag
      selected = row.displayFlag
      console.log(row.displayFlag);
      return selected;
    },
    handleDelete(val) {
      this.$confirm("此操作将永久删除该条数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        customClass: "myClass"
      })
        .then(() => {
          dataModificationApi.numberDel(val).then(res => {
            if (res.code === 1) {
              this.$message.success(res.msg);
              this.search();
            } else {
              this.$message.warning(res.msg || "删除失败");
            }
          });
        })
        .catch(() => {
          // this.$message({
          //   type: "info",
          //   message: "已取消删除"
          // });
        });
    },
    save() {
      this.$confirm("此操作将初始化, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        customClass: "myClass"
      })
        .then(() => {
          // 新增接口
          dataModificationApi.addConfig({ traceType: this.topitemtypeid.type, farmingTypeNo: sessionStorage.farmingTypeNo, farming: sessionStorage.farming || 'BREED' }).then(res => {
            if (res.code === 1) {
              this.$message.success(res.msg || "初始化成功！");
              this.search();
              // this.$refs[formData].clearValidate();
            } else {
              this.$message.error(res.msg || "初始化失败！");
            }
          });
        })
        .catch(() => {
          // this.$message({
          //   type: "info",
          //   message: "已取消删除"
          // });
        });
    }
  }
};
</script>

<style scoped lang="scss">
@import "~@/styles/variables.scss";

</style>
